<?php

include("modules/search/global.php");

$links=" > Search";
$pagetitle=" - Search";

include("header.php");

if(!$forumkeyword&&!$memberkeyword){
	unset($tplcontent);
	
	$tplcontent['formheader'].="<form action=\"modules.php?module=search\" method=\"post\">";
	
	$tplcontent['forumkeyword'].="<input type=text name=\"forumkeyword\" size=40 class=\"forminput\">";
	
	$tplcontent['forumexactphrase'].="<input type=checkbox name=\"forumexactphrase\" class=\"forminput\">";
	$tplcontent['forumcasesensitive'].="<input type=checkbox name=\"forumcasesensitive\" class=\"forminput\">";
	$tplcontent['forumallwords'].="<input type=checkbox name=\"forumallwords\" class=\"forminput\">";
	
	$tplcontent['forumlookin'].="<select name=\"forumlookin\" class=\"forminput\">";
	
	$tplcontent['forumlookin'].="<option value=\"0\">".$languagedef['search'][25]."</option>";
	$tplcontent['forumlookin'].=forumsmenu(0,1);
	
	$tplcontent['forumlookin'].="</select>";
	
	$tplcontent['forumpart'].="<select name=\"forumpart\" class=\"forminput\">";
	
	$tplcontent['forumpart'].="<option value=\"body\">".$languagedef['search'][26]."</option>";
	$tplcontent['forumpart'].="<option value=\"subject\">".$languagedef['search'][27]."</option>";
	//$tplcontent['forumpart'].="<option value=\"username\">User (Name)</option>";
	$tplcontent['forumpart'].="<option value=\"userid\">".$languagedef['search'][28]."</option>";
	$tplcontent['forumpart'].="<option value=\"datenormal\">".$languagedef['search'][29]."</option>";
	$tplcontent['forumpart'].="<option value=\"datetimestamp\">".$languagedef['search'][30]."</option>";
	//$tplcontent['forumpart'].="<option value=\"topicnumber\">Topic Number</option>";
	
	$tplcontent['forumpart'].="</select>";
	
	$tplcontent['memberkeyword'].="<input type=text name=\"memberkeyword\" size=40 class=\"forminput\">";
	
	$tplcontent['membercasesensitive'].="<input type=checkbox name=\"membercasesensitive\" class=\"forminput\">";
	
	$tplcontent['memberpart'].="<select name=\"memberpart\" class=\"forminput\">";
	
	//$tplcontent['memberpart'].="<option value=\"username\">User Name</option>";
	$tplcontent['memberpart'].="<option value=\"displayname\">".$languagedef['search'][31]."</option>";
	$tplcontent['memberpart'].="<option value=\"email\">".$languagedef['search'][24]."</option>";
	$tplcontent['memberpart'].="<option value=\"group\">".$languagedef['search'][22]."</option>";
	$tplcontent['memberpart'].="<option value=\"registerednormal\">".$languagedef['search'][32]."</option>";
	$tplcontent['memberpart'].="<option value=\"registeredtimestamp\">".$languagedef['search'][33]."</option>";
	$tplcontent['forumpart'].="</select>";
	
	$tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Search!\" class=\"formbutton\">";
	
	$tplcontent['formfooter'].="</form>";
	
	
	echo template('modules/search/searchkeyword',$tplcontent);
}

if($forumkeyword||$memberkeyword){
	
	if($forumkeyword&&$memberkeyword){
		echo template('modules/search/noticeeithersearch',$tplcontent);
	}else{
		
		//search forums and begin results table
		if($forumkeyword){
			
			if($forumexactphrase=="on"||$forumpart=="datenormal"||$forumpart=="datetimestamp"||$forumpart=="userid"){
				$searchfor2[0]=$forumkeyword;
				$searchforvalue2[0]=1;
			}else{
				$searchfor1=explode(" ",$forumkeyword);
				$searchforvalue1=array();
				$searchforvalue1=array_pad($searchforvalue1,count($searchfor1),1);
				$searchfor2=array();
				$searchforvalue2=array();
				
				//start with 2 keywords, create string, then 3 keywords, create string, etc until all keywords used
				for($n=2;$n<(count($searchfor1)+1);$n++){
					
					unset($searchforadd);
					for($m=0;$m<$n;$m++){
						$searchforadd.=$searchfor1[$m]." ";
					}
					
					array_unshift($searchfor2,trim($searchforadd));
					array_unshift($searchforvalue2,(count($searchfor1)+count($searchfor2)));
					
				}
				
			}
			
			unset($forumarray2);
			//if not all forums then only look in selected forum
			if($forumlookin!=="0"){
				$forumarray2[0]=$forumlookin;
			}else{
				$forumarray2=$forumarray;
			}
			
			switch ($forumpart){
				case 'datetimestamp':
				case 'datenormal':{
					$k=1; break;
				}
				case 'username':
				case 'userid':{
					$k=0; break;
				}
				case 'subject':{
					$k=2; break;
				}
				case 'body':{
					$k=3; break;
				}
			}
			
			for($n=0;$n<count($forumarray2);$n++){
				$topicarray=listdirs("$configarray[2]/$forumarray2[$n]");
				
				for($m=0;$m<count($topicarray);$m++){
					$postarray=listfiles("$configarray[2]/$forumarray2[$n]/$topicarray[$m]");
					
					for($l=0;$l<count($postarray);$l++){
						$indpostarray=getdata("$configarray[2]/$forumarray2[$n]/$topicarray[$m]/$postarray[$l].php");
						
						unset($relevancevalue);
						
						//search through compiled phrases
						for($p=0;$p<count($searchfor2);$p++){
							
							if($forumpart=="datenormal"){
								$indpostarray[$k]=date("m/d/Y",$indpostarray[$k]);
							}
							
							if($forumcasesensitive=="on"){
								if(strstr($indpostarray[$k],$searchfor2[$p])){
									$relevancevalue+=$searchforvalue2[$p];
									break 1;
								}
							}else{
								if(stristr($indpostarray[$k],$searchfor2[$p])){
									$relevancevalue+=$searchforvalue2[$p];
									break 1;
								}
							}
						}
						
						//search through each keyword in phrase (any comination basically)
						if($relevancevalue<1&&(count($searchfor1)==1||$forumallwords=="on")){
							
							for($p=0;$p<count($searchfor1);$p++){
								
								if($forumcasesensitive=="on"){
									if(strstr($indpostarray[$k],$searchfor1[$p])){
										$relevancevalue+=$searchforvalue1[$p];
									}
								}else{
									if(stristr($indpostarray[$k],$searchfor1[$p])){
										$relevancevalue+=$searchforvalue1[$p];
									}
								}
							}
						}
						
						if($relevancevalue>0){
							$searcharray[]="$relevancevalue\t$forumarray2[$n]\t$topicarray[$m]\t$postarray[$l]";
						}
						
					}
					
				}
				
			}
			
			//BEGIN DISPLAY RESULTS
			unset($tplcontent);
			
			$tplcontent['keyword'].=stripslashes($forumkeyword);
			$tplcontent['lookin'].=$forumlookin;
			$tplcontent['part'].=$forumpart;
			
			@rsort($searcharray);
			
			if(!$page){$page=1;}
			
			for($n=($page-1)*$searchmoduleconfig[0];$n<count($searcharray)&&$n<(($page-1)*$searchmoduleconfig[0])+$searchmoduleconfig[0];$n++){
				unset($tplcontent2);
				
				$searcharray2=explode("\t",$searcharray[$n]);
				$firstpostarray=getdata("$configarray[2]/$searcharray2[1]/$searcharray2[2]/0.php");
				
				$firstpostarray[2]=htmlentities($firstpostarray[2]);
				
				$topicnum=topic_timetonum($searcharray2[1],$searcharray2[2]);

				$relevance=(round(($searcharray2[0]/(count($searchfor1)+count($searchfor2))),2)*100);
				
				$tplcontent2['relevance'].=$relevance."%";

				$tplcontent2['topictitle'].="<a href=\"index.php?forum=$searcharray2[1]&topic=$topicnum&highlight=$forumkeyword#$searcharray2[3]\">$firstpostarray[2]</a>";

				
				$userarray=getdata("$configarray[1]/$firstpostarray[0]/main.php");
				
				if(count($userarray)>0){
					$tplcontent2['topicstarter'].="<a href=\"profile.php?user=$firstpostarray[0]\">$userarray[0]</a>";
				}else{
					$tplcontent2['topicstarter'].="Guest";
				}
				
				$forumconfigarray=getdata("$configarray[2]/$searcharray2[1].php");
				$tplcontent2['forum'].=$forumconfigarray[3];
				
				$tplcontent['searchrows'].=template('modules/search/searchforumsrow',$tplcontent2);
				
			}
			
			$page2=1;
			
			for($n=0;$n<count($searcharray);$n+=$searchmoduleconfig[0]){
				
				if($n==($page-1)*$searchmoduleconfig[0]) {
					$tplcontent['page'].="<b>$page2</b> ";
				}else{
					$tplcontent['page'].="<a href=\"modules.php?module=search&forumkeyword=$forumkeyword&forumlookin=$forumlookin&forumpart=$forumpart&forumallwords=$forumallwords&page=$page2\">$page2</a> ";
				}
				
				$page2++;
			}
			
			echo template('modules/search/searchforums',$tplcontent);
			
		}//end forum search
		
		if($memberkeyword){
			//search members and begin results table
			
			switch ($memberpart){
				case 'displayname':{
					$k=0; break;
				}
				case 'email':{
					$k=2; break;
				}
				case 'registerednormal':{
					$k=4; break;
				}
				case 'registeredtimestamp':{
					$k=4; break;
				}
				case 'group':{
					$k=15; break;
				}
			}
			
			for($m=0;$m<count($usersarray);$m++){
				
				$userarray=getdata("$configarray[1]/$usersarray[$m]/main.php");
				
				if($memberpart=="registerednormal"){
					echo $userarray[$k]."<br>";
					$userarray[$k]=date("m/d/Y",$userarray[$k]);
					echo $userarray[$k]."<br>";
				}
				
				if($membercasesensitive=="on"){
					if(strstr($userarray[$k],$memberkeyword)){
						$searcharray[]=$usersarray[$m];
					}
				}else{
					if(stristr($userarray[$k],$memberkeyword)){
						$searcharray[]=$usersarray[$m];
					}
				}
			}
			
			//START DISPLAY RESULTS
			unset($tplcontent);
			
			$tplcontent['keyword'].=stripslashes($memberkeyword);
			$tplcontent['part'].=$memberpart;
			
			if(!$page){$page=1;}
			
			for($n=($page-1)*$searchmoduleconfig[0];$n<count($searcharray)&&$n<(($page-1)*$searchmoduleconfig[0])+$searchmoduleconfig[0];$n++){
				unset($tplcontent2);
				
				$userarray=getdata("$configarray[1]/$searcharray[$n]/main.php");
				
				$tplcontent2['displayname'].="<a href=\"profile.php?user=$searcharray[$n]\">$userarray[0]</a>";
				$tplcontent2['group'].=$userarray[15];
				$tplcontent2['regdate'].=date($dateformat,$userarray[4]);
				
				$emailarray=explode("\t",$userarray[2]);
				if($emailarray[1]=="hide"){
					$tplcontent2['email'].="hidden";
				}else{
					$tplcontent2['email'].=$emailarray[0];
				}
			
				$tplcontent['searchrows'].=template('modules/search/searchmembersrow',$tplcontent2);
				
			}
			
			$page2=1;
			
			for($n=0;$n<count($searcharray);$n+=$searchmoduleconfig[0]){
				
				if($n==($page-1)*$searchmoduleconfig[0]) {
					$tplcontent['page'].="<b>$page2</b> ";
				}else{
					$tplcontent['page'].="<a href=\"modules.php?module=search&memberkeyword=$memberkeyword&memberpart=$memberpart&page=$page2\">$page2</a> ";
				}
				
				$page2++;
			}
			
			echo template('modules/search/searchmembers',$tplcontent);
		}

	}
	
}


require ("footer.php");
?>
